<!--
 * @Date: 2022-11-21 15:10:35
 * @LastEditTime: 2022-12-27 14:07:53
 * @Description: content
-->
<template>
  <view class="mall-container">
    <!-- Top banner -->
    <view class="mall-header">
      <image
        class="mall-banner"
        src="/static/images/mall_header.png"
        mode="widthFix"
      ></image>
    </view>

    <!-- Search bar -->
    <view class="search-bar">
      <view class="search-input-container">
        <input
          type="text"
          v-model="searchKeywords"
          placeholder="输入内容"
          class="search-input"
        />
        <img
          src="/static/images/hg_srarch.png"
          alt=""
          class="search-icon"
		  @click="searchClick"
        />
      </view>
      <view class="cart-btn" @tap="goToCart">
        <img src="/static/images/mall_gwc.png" alt="" />
      </view>
    </view>

    <!-- Product list -->
    <view class="product-list">
      <scroll-view
        scroll-y
        class="scroll-view"
        @scrolltolower="loadMore"
        lower-threshold="50"
      >
        <view class="product-grid">
          <view
            v-for="(item, index) in goodsList"
            :key="index"
            class="product-item"
            @tap="goToDetail(item)"
          >
            <!-- Using background color instead of image -->
            <view class="product-img">
              <img :src="item.thumb" alt="" />
            </view>
            <view class="product-info">
              <view class="product-name">{{ item.title }}</view>
              <view class="product-id">
                {{ item.price }}
                <text class="product-jf">积分</text>
              </view>
            </view>
          </view>
        </view>
        <view v-if="loading" class="loading">加载中...</view>
        <view v-if="noMore" class="no-more">没有更多数据了</view>
      </scroll-view>
    </view>
    <custom-tabbar :current="1" @change="handleTabChange"></custom-tabbar>
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [], // Create 9 empty product items
      searchKeywords: "",
      page: 1,
      per_page: 10,
      sort_type: 1,
      loading: false,
      noMore: false,
      hasMore: true,
    };
  },

  onLoad() {
    this.getList();
  },

  onPullDownRefresh() {
    this.refreshList();
  },

  methods: {
    searchClick() {
		this.refreshList();
    },
    handleTabChange() {},

    handleSearch() {
      this.refreshList();
    },

    refreshList() {
      this.goodsList = [];
      this.page = 1;
      this.noMore = false;
      this.hasMore = true;
      this.getList();
      uni.stopPullDownRefresh();
    },

    loadMore() {
      if (this.loading || !this.hasMore) return;
      this.page++;
      this.getList();
    },

    goToDetail(item) {
      uni.navigateTo({
        url: `/pages/mall/detail?id=${item.id}`,
      });
    },

    goToCart() {
      uni.navigateTo({
        url: "/pages/cart/cart",
      });
    },

    getList() {
      if (this.loading) return;

      this.loading = true;

      this.req({
        url: "/v1/shop/list",
        data: {
          keywords: this.searchKeywords,
          page: this.page,
          per_page: this.per_page,
          sort_type: this.sort_type,
        },
        Loading: false,
        success: (res) => {
          if (res.code == 200) {
            const newData = res.data.data || [];

            if (this.page === 1) {
              this.goodsList = newData;
            } else {
              this.goodsList = [...this.goodsList, ...newData];
            }

            this.hasMore = newData.length >= this.per_page;
            this.noMore = newData.length < this.per_page;
          }
        },
        complete: () => {
          this.loading = false;
        },
      });
    },
  },
};
</script>

<style lang="scss">
.mall-container {
  background-color: #f0dbb0;
  min-height: 100vh;
  padding-bottom: 30rpx;
}

.mall-header {
  .mall-banner {
    width: 100%;
    height: 80rpx;
  }
}

.search-bar {
  margin: 0 30rpx 20rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .search-input-container {
    flex: 1;
    display: flex;
    align-items: center;
    background-color: #efe6cd;
    border-radius: 40rpx;
    padding: 10rpx 20rpx;
    margin-right: 20rpx;
    border: 2rpx solid #000;
  }

  .search-icon {
    color: #999;
    font-size: 28rpx;
    margin-right: 10rpx;
    width: 60rpx;
    height: 60rpx;
  }

  .search-input {
    flex: 1;
    height: 60rpx;
    font-size: 28rpx;
    border: none;
    background: transparent;
  }

  .cart-btn {
    width: 100rpx;
    height: 100rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 30rpx;
    font-size: 24rpx;
    color: #333;
    img {
      width: 100%;
      height: 100%;
    }
  }
}

.product-list {
  height: calc(100vh - 220rpx);
  padding: 0 20rpx;

  .scroll-view {
    height: 100%;
  }

  .product-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .product-item {
    width: 30%;
    margin-bottom: 20rpx;
    overflow: hidden;
    background: #ffffff;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    border: 4rpx solid #000000;

    .product-img {
      height: 220rpx;
      background-color: #f0f0f0; /* Use background color instead of image */

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .product-info {
      padding: 10rpx;

      .product-name {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-weight: 600;
        font-size: 22rpx;
        color: #000000;
        line-height: 26rpx;
      }

      .product-id {
        font-size: 22rpx;
        color: #999;
        margin-top: 6rpx;
        font-weight: 500;
        font-size: 24rpx;
        color: #000000;
        line-height: 28rpx;
        margin-top: 8rpx;
      }
      .product-jf{
        margin-left: 5rpx;
        font-size: 20rpx;
        color: #000;
      }
    }
  }

  .loading,
  .no-more {
    text-align: center;
    padding: 20rpx 0;
    font-size: 24rpx;
    color: #999;
  }
}
</style>
